package com.torlesse.producer.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/02/09/20:27
 * @Description:
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 创建交换机
     * @return
     */
    @Bean(name = "itemTopicExchange")
    public Exchange createTopicExchange(){
        return ExchangeBuilder.topicExchange("item_topic_exchange").build();
    }

    /**
     * 创建消息队列
     * @return
     */
    @Bean(name = "itemQueue")
    public Queue createItemQueue(){
        return QueueBuilder.durable("item_queue").build();
    }

    /**
     * 队列绑定交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding bindingQueueToExchange(@Qualifier("itemQueue") Queue queue,
                                          @Qualifier("itemTopicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
    }
}
